package com.ming.yourdiary.fragment;

import android.os.AsyncTask;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ming.yourdiary.R;
import com.ming.yourdiary.adapter.DiaryCardAdapter;
import com.ming.yourdiary.entity.DiaryNote;
import com.ming.yourdiary.util.DataBaseUtil;
import com.ming.yourdiary.util.TestDataUtil;
import com.ming.yourdiary.view.DiaryNoteDecoration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.DateUtil;

public class DiaryNotesFragment extends Fragment {

  /*View*/
  private RecyclerView mNotesRecyclerView;
  private TextView mLoadingTips;
  private RecyclerView.ItemDecoration mDiaryNoteCardDecoration;

  /*Adapter*/
  private DiaryCardAdapter mDiaryCardAdapter;
  /*Data*/
  private List<DiaryCardAdapter.DiaryNotesItem> mDiaryNotes = Collections.synchronizedList(new ArrayList<DiaryCardAdapter.DiaryNotesItem>());


  private Handler mHandler = new Handler();

  private boolean mIsFirstIn;

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mDiaryCardAdapter = new DiaryCardAdapter(getActivity(), mDiaryNotes);
  }

  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_mian_diary_notes, container, false);
    mNotesRecyclerView = (RecyclerView) view.findViewById(R.id.diary_notes_list);
    mLoadingTips = (TextView) view.findViewById(R.id.loading_tips);
    initRecyclerView();
    return view;
  }

  private void initRecyclerView() {
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    mNotesRecyclerView.setLayoutManager(linearLayoutManager);
    mDiaryNoteCardDecoration =
      new DiaryNoteDecoration(0, getActivity().getResources().getDimensionPixelOffset(R.dimen.dp_10));
    mNotesRecyclerView.addItemDecoration(mDiaryNoteCardDecoration);
    mNotesRecyclerView.setAdapter(mDiaryCardAdapter);
    new RefreshDataTask().execute();
  }

  //获取日记记录
  public void getDiaryNotes() {
    //TODO 测试用 避免重复插入数据
    DataBaseUtil.clear();
    //插入数据
    DataBaseUtil.insertNotes(TestDataUtil.generateTestDiaryNotes());
    mDiaryNotes.clear();
    //查询数据
    Map<String, List<DiaryNote>> result = DataBaseUtil.searchOneYear(0);
    sortDiaryNotes(result, mDiaryNotes);
    mDiaryCardAdapter.setData(mDiaryNotes);
  }

  private void sortDiaryNotes(Map<String, List<DiaryNote>> result, List<DiaryCardAdapter.DiaryNotesItem> diaryNotes) {
    for (Map.Entry<String, List<DiaryNote>> entry : result.entrySet()) {
      //添加月份标题
      DiaryCardAdapter.DiaryNotesItem monthItem = new DiaryCardAdapter.DiaryNotesItem();
      monthItem.monthTitle = entry.getKey();
      monthItem.type = DiaryCardAdapter.DiaryNotesItem.ITEM_TYPE_TITLE;
      diaryNotes.add(monthItem);
      //添加月份中的记录
      for (DiaryNote note : entry.getValue()) {
        DiaryCardAdapter.DiaryNotesItem diaryNotesItem = new DiaryCardAdapter.DiaryNotesItem();
        diaryNotesItem.diaryNote = note;
        diaryNotesItem.type = DiaryCardAdapter.DiaryNotesItem.ITEM_TYPE_CARD;
        diaryNotes.add(diaryNotesItem);
      }
    }
  }


  //异步刷新获取数据
  class RefreshDataTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
      super.onPreExecute();
      showLoadingTips();
    }

    @Override
    protected Void doInBackground(Void... voids) {
      getDiaryNotes();
      return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
      super.onPostExecute(aVoid);
      showNotes();
    }
  }

  //加载状态--显示loading
  public void showLoadingTips() {
    if (mIsFirstIn) {
      mLoadingTips.setVisibility(View.VISIBLE);
      mIsFirstIn = false;
    }
  }

  //加载完成--展示列表
  public void showNotes() {
    if (mDiaryCardAdapter != null) {
      mLoadingTips.setVisibility(View.GONE);
      mDiaryCardAdapter.notifyDataSetChanged();
    }
  }


}
